package com.ynsj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynsj.domain.SysUser;
import com.ynsj.model.R;
import com.ynsj.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

@RestController
@Api(tags = "员工管理")
@RequestMapping("/users")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    @ApiOperation("分页查询员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "页大小"),
            @ApiImplicitParam(name = "mobile",value = "手机号"),
            @ApiImplicitParam(name = "fullname",value = "姓名")
    })
    @PreAuthorize("hasAuthority('sys_user_query')")
    public R<Page<SysUser>> findSysUser(@ApiIgnore Page<SysUser> sysUserPage, String mobile, String fullname){
        return R.ok(sysUserService.findByPage(sysUserPage,mobile,fullname));
    }

    @PostMapping
    @ApiOperation("新增员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUser",value = "用户数据对象")
    })
    @PreAuthorize("hasAuthority('sys_user_create')")
    public R addSysUser(@RequestBody @Validated SysUser sysUser){
        boolean isOk = sysUserService.addSysUser(sysUser);
        if (isOk){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @PostMapping("/delete")
    @ApiOperation("删除员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "删除的用户id")
    })
    @PreAuthorize("hasAuthority('sys_user_delete')")
    public R delSysUser(@RequestBody Long[] ids){
        boolean isOk = sysUserService.removeByIds(Arrays.asList(ids));
        if (isOk){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @PatchMapping
    @ApiOperation("修改员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUser",value = "用户数据对象")
    })
    @PreAuthorize("hasAuthority('sys_user_update')")
    public R updateSysUser(@RequestBody @Validated SysUser sysUser){
        boolean isOk = sysUserService.updateSysUser(sysUser);
        if (isOk){
            return R.ok("更新成功");
        }
        return R.fail("更新失败");
    }
}
